include ../../makedef.mk

libsrc := $(wildcard lib*.c)
libtarget := $(patsubst %.c,%.o,${libsrc})

GCCFLAGS := ${GCCFLAGS} ${GCCANALYZER} -Wno-error=analyzer-possible-null-dereference \
			-Wno-error=analyzer-malloc-leak -Wno-error=analyzer-null-dereference 

ifeq ($(uname_s),Darwin)
	GCCFLAGS := 
endif

CFLAGS := ${CFLAGS} -D_REENTRANT -DRUNTIME_DBG_LVL=0
CFLAGS_ACO := -D_REENTRANT -DRUNTIME_DBG_LVL=0

GIT3RD := ../git3rd

# 恢复sse和fpuctrl
ifeq ($(uname_m),x86_64)
	CFLAGS := ${CFLAGS}
	# -DRESUME_SSE -DRESUME_FPUCTRL

	MPROMPT_ASM := ${GIT3RD}/libmprompt/src/mprompt/asm/longjmp_amd64.S
	MPROMPT_OPT :=  ${COPT_FLAGS}
	CONCURRENT_ASM := ${GIT3RD}/libconcurrent/src/arch/x86_64/concurrent_arch.o

	COMPILE_TARGET := prompt cocoro cocomi aco concurrent co
endif

# mprompt aarch64 O3优化似乎会导致问题? 
ifeq ($(uname_m),aarch64)
	CFLAGS := ${CFLAGS}
	#-DRESUME_FPU
	
	MPROMPT_ASM := ${GIT3RD}/libmprompt/src/mprompt/asm/longjmp_arm64.S
	MPROMPT_OPT := 
	CONCURRENT_ASM := ${GIT3RD}/libconcurrent/src/arch/aarch64/concurrent_arch.o
	
	COMPILE_TARGET := prompt cocoro cocomi concurrent
endif

ifeq ($(uname_m),arm64)
	CFLAGS := ${CFLAGS}
	#-DRESUME_FPU
	
	MPROMPT_ASM := ${GIT3RD}/libmprompt/src/mprompt/asm/longjmp_arm64.S
	MPROMPT_OPT :=
	CONCURRENT_ASM := ${GIT3RD}/libconcurrent/src/arch/arm64/concurrent_arch.o
	
	COMPILE_TARGET := prompt cocoro cocomi concurrent
endif


#mac osx 10.6 不再支持ucontext接口
ifneq ($(uname_s),Darwin)
	COMPILE_TARGET := ${COMPILE_TARGET} coroutine
endif


CPPFLAGS := -D_GNU_SOURCE -D_REENTRANT

SOFLAGS := -fPIC -shared -ftls-model=initial-exec

LIBINC := -I../../include/ -I../../include_api/ -I../../include_arch/ -I../../include_plat/

.PHONY:clean all run

all:${COMPILE_TARGET}
	@rm -f *.plist

# ${CLANG} -fPIC -shared -ftls-model=initial-exec ${INC} -I../libmprompt/include ../libmprompt/out/release/libmprompt.a ../libmprompt/src/mprompt/util.c  ../libmprompt/src/mprompt/asm/longjmp_amd64.S ../libmprompt/src/mprompt/gstack.c ../libmprompt/src/mprompt/mprompt.c crout_prompt.c ${COPT_FLAGS} -latomic -o libcrout_mprompt.so
co:
	@make clean -C ${GIT3RD}/libco
	@make -C ${GIT3RD}/libco

prompt:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@#${CLANG} ${LIBINC} -I${GIT3RD}/libmprompt/include ${GIT3RD}/libmprompt/src/mprompt/util.c  ${MPROMPT_ASM} ${GIT3RD}/libmprompt/src/mprompt/gstack.c ${GIT3RD}/libmprompt/src/mprompt/mprompt.c mprompt.c ${CFLAGS} ${MPROMPT_OPT} -DMP_USE_C=ON -c -o ../build_lib/cllibcoco_mprompt.a
	@#${GCC} ${LIBINC} -I${GIT3RD}/libmprompt/include ${GIT3RD}/libmprompt/src/mprompt/util.c  ${MPROMPT_ASM} ${GIT3RD}/libmprompt/src/mprompt/gstack.c ${GIT3RD}/libmprompt/src/mprompt/mprompt.c mprompt.c ${CFLAGS} ${MPROMPT_OPT}  -DMP_USE_C=ON -c -o ../build_lib/glibcoco_mprompt.a
	@${CLANG} ${SOFLAGS} ${LIBINC} -I${GIT3RD}/libmprompt/include ${GIT3RD}/libmprompt/src/mprompt/util.c  ${MPROMPT_ASM} ${GIT3RD}/libmprompt/src/mprompt/gstack.c ${GIT3RD}/libmprompt/src/mprompt/mprompt.c mprompt.c ${CFLAGS} ${MPROMPT_OPT} -DMP_USE_C=ON -o ../build_libso/cllibcoco_mprompt.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC} -I${GIT3RD}/libmprompt/include ${GIT3RD}/libmprompt/src/mprompt/util.c  ${MPROMPT_ASM} ${GIT3RD}/libmprompt/src/mprompt/gstack.c ${GIT3RD}/libmprompt/src/mprompt/mprompt.c mprompt.c ${CFLAGS} ${MPROMPT_OPT}  -DMP_USE_C=ON -o ../build_libso/glibcoco_mprompt.${sharelib}

cocoro:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@${CLANG} ${LIBINC} cocoro.c ${CFLAGS} ${COPT_FLAGS} -c -o ../build_lib/cllibcoco_cocoro.a
	@${GCC}  ${LIBINC} cocoro.c ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -c -o ../build_lib/glibcoco_cocoro.a
	@${CLANG} ${SOFLAGS} ${LIBINC} cocoro.c ${CFLAGS} ${COPT_FLAGS} -o ../build_libso/cllibcoco_cocoro.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC} cocoro.c ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -o ../build_libso/glibcoco_cocoro.${sharelib}

cocomi:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@${CLANG} ${LIBINC} cocomi.c ${CFLAGS} ${COPT_FLAGS} -c -o ../build_lib/cllibcoco_cocomi.a
	@${GCC}  ${LIBINC} cocomi.c ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -c -o ../build_lib/glibcoco_cocomi.a
	@${CLANG} ${SOFLAGS} ${LIBINC} cocomi.c ${CFLAGS} ${COPT_FLAGS} -o ../build_libso/cllibcoco_cocomi.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC} cocomi.c ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -o ../build_libso/glibcoco_cocomi.${sharelib}

coroutine:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@${CLANG} ${LIBINC}  -I${GIT3RD}/coroutine -include ${GIT3RD}/coroutine/coroutine.c coroutine.c ${CFLAGS} ${COPT_FLAGS} -c -o ../build_lib/cllibcoco_coroutine.a
	@${GCC}  ${LIBINC}  -I${GIT3RD}/coroutine -include ${GIT3RD}/coroutine/coroutine.c coroutine.c ${CFLAGS} ${COPT_FLAGS}  -c -o ../build_lib/glibcoco_coroutine.a
	@${CLANG} ${SOFLAGS} ${LIBINC}  -I${GIT3RD}/coroutine coroutine.c ${GIT3RD}/coroutine/coroutine.c ${CFLAGS} ${COPT_FLAGS} -o ../build_libso/cllibcoco_coroutine.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC}  -I${GIT3RD}/coroutine coroutine.c ${GIT3RD}/coroutine/coroutine.c ${CFLAGS} ${COPT_FLAGS}  -o ../build_libso/glibcoco_coroutine.${sharelib}

s_task:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@#${CLANG} ${LIBINC}  -I${GIT3RD}/s_task/include  -include ${GIT3RD}/coroutine/coroutine.c coroutine.c ${CFLAGS} ${COPT_FLAGS} -c -o ../build_lib/cllibcoco_s_task.a
	@#${GCC}  ${LIBINC}  -I${GIT3RD}/s_task/include  -include ${GIT3RD}/coroutine/coroutine.c coroutine.c ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -c -o ../build_lib/glibcoco_s_task.a
	@${CLANG} ${SOFLAGS} ${LIBINC}  -I${GIT3RD}/s_task/include s_task.c ${GIT3RD}/s_task/src/s_task.c ../s_task/src/s_list.c ../s_task/src/s_rbtree.c ${CFLAGS} ${COPT_FLAGS} -o ../build_libso/cllibcoco_s_task.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC}  -I${GIT3RD}/s_task/include  s_task.c ${GIT3RD}/s_task/src/s_task.c ../s_task/src/s_list.c ../s_task/src/s_rbtree.c ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -o ../build_libso/glibcoco_s_task.${sharelib}

aco:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@#${CLANG} ${LIBINC} -I${GIT3RD}/libaco aco.c ${GIT3RD}/libaco/aco.c ${GIT3RD}/libaco/acosw.S  ${CFLAGS_ACO} ${COPT_FLAGS} -c -o ../build_lib/cllibcoco_aco.a
	@#${GCC} ${LIBINC} -I${GIT3RD}/libaco aco.c ${GIT3RD}/libaco/aco.c ${GIT3RD}/libaco/acosw.S ${CFLAGS_ACO} ${COPT_FLAGS} ${GCCFLAGS} -c -o ../build_lib/glibcoco_aco.a
	@${CLANG} ${SOFLAGS} ${LIBINC} -I${GIT3RD}/libaco aco.c ${GIT3RD}/libaco/aco.c ${GIT3RD}/libaco/acosw.S ${CFLAGS_ACO} ${COPT_FLAGS} -o ../build_libso/cllibcoco_aco.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC} -I${GIT3RD}/libaco aco.c ${GIT3RD}/libaco/aco.c ${GIT3RD}/libaco/acosw.S ${CFLAGS_ACO} ${COPT_FLAGS} ${GCCFLAGS} -o ../build_libso/glibcoco_aco.${sharelib}


concurrent:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	@# 先编译nasm源码
	@make clean -C ${GIT3RD}/libconcurrent 
	@make -C ${GIT3RD}/libconcurrent 
	@#${CLANG} ${LIBINC} -I${GIT3RD}/libconcurrent/include -include ${GIT3RD}/libconcurrent/src/concurrent.c  concurrent.c ${CONCURRENT_ASM}  ${CFLAGS} ${COPT_FLAGS} -c -o ../build_lib/cllibcoco_concurrent.a
	@#${GCC} ${LIBINC} -I${GIT3RD}/libconcurrent/include -include ${GIT3RD}/libconcurrent/src/concurrent.c  concurrent.c ${CONCURRENT_ASM} ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -c -o ../build_lib/glibcoco_concurrent.a
	@${CLANG} ${SOFLAGS} ${LIBINC} -I${GIT3RD}/libconcurrent/include -include ${GIT3RD}/libconcurrent/src/concurrent.c  concurrent.c ${CONCURRENT_ASM} ${CFLAGS} ${COPT_FLAGS} -o ../build_libso/cllibcoco_concurrent.${sharelib}
	@${GCC} ${SOFLAGS} ${LIBINC} -I${GIT3RD}/libconcurrent/include -include ${GIT3RD}/libconcurrent/src/concurrent.c  concurrent.c ${CONCURRENT_ASM} ${CFLAGS} ${COPT_FLAGS} ${GCCFLAGS} -o ../build_libso/glibcoco_concurrent.${sharelib}



clean:
	@mkdir -p ../build_libso
	@mkdir -p ../build_lib
	rm -rf ../build_lib
	rm -rf ../build_libso 
	
lib%.o:lib%.c
	@echo "----generated pre-processed $@.cpp----"
	@${GCC} ${LIBINC} $^ -E  > ./h$@.cpp
	@echo "----run clang static analyzer----"
	@${CLANG} ${LIBINC} --analyze $^
	@echo "----compiling static lib using gcc with analyzer----"
	@${GCC} ${LIBINC} $^ ${COPT_FLAGS} ${CFLAGS} ${GCCFLAGS} -c -o glib$*.a
	@echo "----compiling static lib using clang----"
	@${CLANG} ${LIBINC} $^ ${COPT_FLAGS} ${CFLAGS}  -c -o cllib$*.a
	@echo "----compiling dynamic lib using gcc with analyzer----"
	@${GCC} ${SOFLAGS} ${LIBINC} $^ ${COPT_FLAGS} ${CFLAGS} ${GCCFLAGS} -o glib$*.so
	@echo "----compiling dynamic lib using clang----"
	@${CLANG} ${SOFLAGS} ${LIBINC} $^ ${COPT_FLAGS} ${CFLAGS} -o cllib$*.so
